halcon进行二维码检测

您所在的位置:网站首页 halcon qr码 halcon进行二维码检测

halcon进行二维码检测

2023-10-08 13:39| 来源: 网络整理| 查看: 265

halcon进行二维码检测 原创

茗君(Major_S) 2022-03-03 16:47:37 ©著作权

文章标签 halcon 2d 二维码 去噪 文章分类 后端开发

©著作权归作者所有:来自51CTO博客作者茗君(Major_S)的原创作品,请联系作者获取转载授权,否则将追究法律责任

二维码检测 find_data_code_2d (ImageReduced, SymbolXLDs, DataCodeHandle, ['train'], ['all'], ResultHandles, DecodedDataStrings)

使用halcon进行二维码检测的核心在于​​图像的预处理(去除噪点等)​​和适当算子参数的调整

Halcon检测二维码流程create_data_code_2d_model:创建一个二维码数据class模型set_bar_code_param:设置参数find_data_code_2d:检测和读取图像中的二维数据代码符号clear_data_code_2d_model:删除2D数据代码模型并释放分配的内存。 获取中间结果get_data_code_2d_resultsget_data_code_2d_objectscreate_data_code_2d_model ('QR Code', [], [], DataCodeHandle)read_image (Image, ImageFiles + Index$'.2d')find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)* 1. Display the resultsdev_display (Image)dev_display (SymbolXLDs)disp_message (WindowHandle, 'Image ' + Index + ' of ' + ImageNum, 'window', 12, 12, 'black', 'true')SymbolXLDs:检测出的二维码轮廓 create_data_code_2d_model (‘QR Code’, [], [], DataCodeHandle) ‘QR Code’:二维码的类型 标题if (|DecodedDataStrings|==0) *设置字体颜色 dev_set_color ('red') *设置文字大小 set_display_font (WindowHandle, 30, 'mono', 'true', 'false') *设置文字位置 set_tposition (WindowHandle, 240, 120) *设置文字内容 write_string (WindowHandle, 'NG') stop()endif 二维码识别两个重要点:1.图像预处理;2.二维码算子参数调整 1.图像预处理* 开运算,进行腐蚀,去除噪点gray_opening_shape (Image, ImageOpening, 11, 11, 'octagon') 技巧1:掩膜 技巧2:中值滤波* 中值滤波,去除噪点median_image (Image, ImageMedian, 'circle', 1, 'mirrored') 2.二维码算子参数调整create_data_code_2d_model ('QR Code', 'default_parameters', 'standard_recognition', DataCodeHandle)create_data_code_2d_model ('QR Code', 'default_parameters', 'enhanced_recognition', DataCodeHandle)create_data_code_2d_model ('QR Code', 'default_parameters', 'maximum_recognition', DataCodeHandle)‘standard_recognition’:速度快,准确率低‘enhanced_recognition’:速度慢,准确率高‘maximum_recognition’:速度慢,准确率高small_modules_robustness highfind_data_code_2d (ImageScaled, SymbolXLDs, DataCodeHandle, ['train'], ['polarity'], ResultHandles, DecodedDataStrings)trainallpolarity 面对缩放 其他reduce_domain (, , ImageReduced1)count_seconds (Seconds)trydev_update_off ()dev_close_window ()dev_get_window (WindowHandle)dev_open_window (0, 0, 760, 570, 'black', WindowHandle)set_display_font (WindowHandle, 14, 'mono', 'true', 'false')dev_set_line_width (3)dev_set_color ('green')************************* Step 1: Create a 2d data code model ************************** 创建模型create_data_code_2d_model ('Data Matrix ECC 200', [], [], DataCodeHandle)* 查询模型参数名称query_data_code_2d_params (DataCodeHandle, 'get_model_params', GenParamName)* 获取模型训练前参数get_data_code_2d_param (DataCodeHandle, GenParamName, ModelBeforeTraining)read_data_code_2d_model ('2d_data_code_model.dcm', DataCodeHandle)************************* Step 2: Train the model *************************list_files ('D:/File/二维码识别', 'files', Files)* 读取图像 Files[Index]for Index1 := 0 to |Files|-1 by 1 * Files[Index1]* read_image (Image,'D:\\File\\二维码识别\\8号料_19.bmp')* stop() * 计算开始时间 count_seconds (TStart) * 读取图像 read_image (Image, Files[Index1]) * 中值滤波,去噪* mean_image (Image, ImageMean, 5, 5) * 阈值分割 threshold (Image, Region, 150, 255) * 连通 connection (Region, ConnectedRegions) * 填充 fill_up (ConnectedRegions, RegionFillUp) * 选取特征区域 select_shape (RegionFillUp, SelectedRegions, 'area', 'and', 20000, 999999999) * 区域相减l difference (Image, SelectedRegions, RegionDifference)* draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)* gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2) reduce_domain (Image, RegionDifference, ImageReduced1) * 需要调整参数 * 中值滤波,平滑去噪 mean_image (ImageReduced1, ImageMean1, 7, 7) * 双边滤波,去噪 bilateral_filter (ImageMean1, ImageMean1, ImageBilateral,1, 5, [], []) * gamma变换 gamma_image (ImageBilateral, GammaImage, 0.5, 0.05, 0.5, 255, 'true') * 图像相乘,图像增强 * 第一次判定 mult_image (GammaImage, GammaImage, ImageResult, 0.0300, -500)* reduce_domain (Image, RegionDifference, Rectangle) * 训练 find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings) * 多次判定 if(|DecodedDataStrings| == 0) * 第二次判定 mult_image (GammaImage, GammaImage, ImageResult, 0.0203, -500) find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings) if(|DecodedDataStrings| == 0) * 第三次判定 mult_image (GammaImage, GammaImage, ImageResult, 0.0220, -500) find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings) if(|DecodedDataStrings| == 0) * 第四次判定 mult_image (GammaImage, GammaImage, ImageResult, 0.0360, -500) find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings) if(|DecodedDataStrings| == 0) * 第五次判定 mult_image (GammaImage, GammaImage, ImageResult, 0.021, -500) find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings) endif endif endif endif if(|DecodedDataStrings| == 0) disp_message (WindowHandle, 'NG', 'window', 60, 12, 'red', 'false') stop()* dev_display (Image)* draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)* gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)* reduce_domain (Image, Rectangle, ImageReduced1) * 需要调整参数 * 中值滤波,平滑去噪* mean_image (ImageReduced1, ImageMean1, 5, 5) * 双边滤波,去噪* bilateral_filter (ImageMean1, ImageMean1, ImageBilateral,1, 5, [], []) * gamma变换* gamma_image (ImageBilateral, GammaImage, 0.5, 0.05, 0.5, 255, 'true') * 图像相乘,图像增强 * 第一次判定 0.0212* mult_image (GammaImage, GammaImage, ImageResult, 0.0202, -500)* reduce_domain (Image, RegionDifference, Rectangle) * 训练* find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, ['train'], ['all'], ResultHandles, DecodedDataStrings) endif count_seconds (TFinish) dev_display (ImageResult) dev_display (SymbolXLDs) DuiringTime := (TFinish-TStart)*1000.0 disp_message (WindowHandle, 'Time: '+DuiringTime, 'window', 30, 12, 'white', 'false') disp_message (WindowHandle, DecodedDataStrings, 'window', 60, 12, 'white', 'false') wait_seconds (1) dev_clear_window ()endfor* read_image (Image, TargetPath)* 中值滤波,去噪* mean_image (Image, ImageMean, 5, 5)* 阈值分割* threshold (ImageMean, Region, 150, 255)* 连通* connection (Region, ConnectedRegions)* 填充* fill_up (ConnectedRegions, RegionFillUp)* 选取特征区域* select_shape (RegionFillUp, SelectedRegions, 'area', 'and', 20000, 999999999)* 区域相减l* difference (Image, SelectedRegions, RegionDifference)* 获取图像区域* draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)* gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)* reduce_domain (Image, Rectangle, ImageReduced1)* 需要调整参数* 中值滤波,平滑去噪* mean_image (ImageReduced1, ImageMean1, 7, 7)* 双边滤波,去噪* bilateral_filter (ImageMean1, ImageMean1, ImageBilateral,1, 5, [], [])* gamma变换* gamma_image (ImageBilateral, GammaImage, 0.5, 0.05, 0.5, 255, 'true')* 图像相乘,图像增强* 第一次判定* mult_image (GammaImage, GammaImage, ImageResult, 0.0300, -500)* reduce_domain (Image, RegionDifference, Rectangle)* 训练* find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, ['train'], ['all'], ResultHandles, DecodedDataStrings)* if(|DecodedDataStrings| == 0) * 第二次判定* mult_image (GammaImage, GammaImage, ImageResult, 0.0203, -500)* find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, ['train'], ['all'], ResultHandles, DecodedDataStrings)* if(|DecodedDataStrings| == 0) * 第三次判定* mult_image (GammaImage, GammaImage, ImageResult, 0.0220, -500)* find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, ['train'], ['all'], ResultHandles, DecodedDataStrings)* if(|DecodedDataStrings| == 0) * 第四次判定* mult_image (GammaImage, GammaImage, ImageResult, 0.0360, -500)* find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, ['train'], ['all'], ResultHandles, DecodedDataStrings)* if(|DecodedDataStrings| == 0) * 第五次判定* mult_image (GammaImage, GammaImage, ImageResult, 0.0390, -500)* find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, ['train'], ['all'], ResultHandles, DecodedDataStrings)* endif* endif* endif* endif* get_data_code_2d_param (DataCodeHandle, GenParamName, ModelAfterTraining)* 显示结果* dev_clear_window ()* ModelAdaption := (GenParamName + ':')$'-35' + ModelBeforeTraining$'15' + ' -> ' + ModelAfterTraining* disp_message (WindowHandle, 'Model parameters before and after the training:', 'window', 12, 12, 'white', 'false')* disp_message (WindowHandle, ModelAdaption, 'window', 60, 12, 'white', 'false')* disp_continue_message (WindowHandle, 'black', 'true')* stop()************************* Step 3: Write the model into a file ************************** write_data_code_2d_model (DataCodeHandle, '2d_data_code_model.dcm')************************* Step 4: Read an existing 2d data code model ************************** read_data_code_2d_model ('2d_data_code_model.dcm', DataCodeHandle)************************* Step 5: Read the data codes ************************** list_files ('D:/File/二维码识别', 'files', Files)* for Index := 0 to |Files|-1 by 1 * 读取图像* read_image (Image, Files[Index]) * 阈值分割* mean_image (Image, ImageMean, 5, 5)* threshold (ImageMean, Region, 150, 255) * 连通* connection (Region, ConnectedRegions) * 填充* fill_up (ConnectedRegions, RegionFillUp) * 选取特征区域* select_shape (RegionFillUp, SelectedRegions, 'area', 'and', 2000, 999999999) * 生产区域* gen_rectange1 (Rectangle, 0, 0, 3072, 4096) * 区域相减l* difference (Image, SelectedRegions, RegionDifference) * 获取图像区域* reduce_domain (Image, RegionDifference, ImageReduced) * 识别二维码* count_seconds (T1)* find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)* count_seconds (T2) * 显示结果* dev_display (ImageReduced)* dev_display (SymbolXLDs)* ConsumeTime := (T2-T1)*1000.0* disp_message (WindowHandle, 'Image ' + 88 + ' of ' + Index, 'window', 12, 12, 'black', 'true')* disp_message (WindowHandle, 'Time:'+ConsumeTime, 'window', 38, 12, 'black', 'true')* disp_message (WindowHandle, '识别结果:'+DecodedDataStrings, 'window', 65, 12, 'black', 'true') * 存入本地txt* open_file ('time.txt', 'append', FileHandle)* fwrite_string (FileHandle, 'Time:'+ConsumeTime)* fnew_line (FileHandle)* close_file (FileHandle) * 判别字符串长度是否为0* if (|DecodedDataStrings|==0) *设置字体颜色* dev_set_color ('blue') *设置文字大小* set_display_font (WindowHandle, 30, 'mono', 'true', 'false') *设置文字位置* set_tposition (WindowHandle, 240, 120) *设置文字内容* write_string (WindowHandle, 'NG')* stop()* endif* stop()* endforcatch (Exception) *设置字体颜色 dev_set_color ('red') *设置文字大小 set_display_font (WindowHandle, 30, 'mono', 'true', 'false') *设置文字位置 set_tposition (WindowHandle, 240, 120) *设置文字内容 write_string (WindowHandle, 'NG') stop()endtry

收藏 评论 分享 举报

上一篇:九点标定和旋转中心标定后旋转点的计算

下一篇:缺陷检测解决策略之六:测量和拟合



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3